<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

if( !isset( $_POST ) ) {
	print( "Don't! Just... don't!" );
	exit();
}
$army_id = getPostValue( 'army_id' );
$query = "SELECT ruler_id, name " .
		"FROM army " .
		"WHERE army_id = '$army_id'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$name = $row[ 'name' ];

if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$query = "SELECT count(*) FROM army_orders WHERE army_id = " . $army_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$rank = $row[ 'count(*)' ];
	
	if( $rank < 6 ) {
		if( $_POST[ 'order_type' ] == 1 ) { // move order
			$x = getPostValue( "x" );
			$y = getPostValue( "y" );
			$village_name = getPostValue( "target_name" );
			$location_id = getPostValue( 'location_id' );
			
			if( $x != "" && $y != "" && is_numeric( $x ) && is_numeric( $y ) ) {
				$query = "INSERT INTO army_orders " .
						"( army_id, order_type, order_value, order_value2, rank ) " .
						"VALUES ( " . $army_id . ", 1, $x, $y, $rank )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );				
				
				$query2 = "SELECT army_id " .
						"FROM army " .
						"WHERE follow = $army_id";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$num2 = mysql_numrows( $result2 );
				$i = 0;
				while ( $i < $num2 ) {
					$query_update = "INSERT INTO army_orders " .
							"( army_id, order_type, order_value, order_value2, rank ) " .
							"VALUES ( " . mysql_result( $result2, $i, 'army_id' ) . ", 1, $x, $y, $rank )";
					mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );				
					$i ++;
				}
			}
			elseif( $village_name != "" ) {
				$query = "SELECT x, y " .
						"FROM village " .
						"WHERE name = '" . $village_name . "'";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				if( isset( $row[ 'x' ] ) ) {
					$query = "INSERT INTO army_orders ( army_id, order_type, order_value, order_value2, rank ) " .
							"VALUES ( " . $army_id . ", 1, " . $row[ 'x' ] . ", " . $row[ 'y' ] . ", $rank )";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );				
					
					$query2 = "SELECT army_id " .
							"FROM army " .
							"WHERE follow = $army_id";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$num2 = mysql_numrows( $result2 );
					$i = 0;
					while ( $i < $num2 ) {
						$query_update = "INSERT INTO army_orders ( army_id, order_type, order_value, order_value2, rank ) " .
								"VALUES ( " . mysql_result( $result2, $i, 'army_id' ) . ", 1, " . $row[ 'x' ] . ", " . $row[ 'y' ] . ", $rank )";
						mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );				
						$i ++;
					}
					
					$status = translate( "Movement order added succesfully." );
				}
				else {
					$status = translate( "No proper target selected." );
				}
			}
			elseif( is_numeric( $location_id ) && $location_id > 0 ) {
				$pass = false;
				$query = "SELECT x, y, ruler_id " .
						"FROM locations " .
						"WHERE location_id = $location_id";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				
				if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
					$pass = true;
				}
				elseif( isset( $row[ 'ruler_id' ] ) ) {	
					$query2 = "SELECT faction_id " .
							"FROM faction_member " .
							"WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					if( isset( $row2[ 'faction_id' ] ) ) {
						$faction_id = $row2[ 'faction_id' ];
					}
					else {
						$faction_id = -1;
					}
					$query2 = "SELECT faction_id " .
							"FROM faction_member " .
							"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					
					if( isset( $row2[ 'faction_id' ] ) && $faction_id == $row2[ 'faction_id' ] ) {
						$pass = true;
					}
				}
				
				if( $pass ) {
					$query = "INSERT INTO army_orders " .
							"( army_id, order_type, order_value, order_value2, rank ) " .
							"VALUES ( " . $army_id . ", 1, " . $row[ 'x' ] . ", " . $row[ 'y' ] . ", $rank )";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );				
					
					$query2 = "SELECT army_id " .
							"FROM army " .
							"WHERE follow = $army_id";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$num2 = mysql_numrows( $result2 );
					$i = 0;
					while ( $i < $num2 ) {
						$query_update = "INSERT INTO army_orders " .
								"( army_id, order_type, order_value, order_value2, rank ) " .
								"VALUES ( " . mysql_result( $result2, $i, 'army_id' ) . ", 1, " . $row[ 'x' ] . ", " . $row[ 'y' ] . ", $rank )";
						mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );				
						$i ++;
					}
					
					$status = translate( "Movement order added succesfully." );
				}
				else {
					$status = translate( "No proper target selected." );
				}
			}
			else {
				$status = translate( "No proper target selected." );
			}
		}
		else if( $_POST[ 'order_type' ] == 2 ) { // wait order
			$wait = getPostValue( "wait_turns" );
			if( is_numeric( $wait ) && $wait > 0 ) {
				$query = "INSERT INTO army_orders ( army_id, order_type, order_value, rank ) " .
						"VALUES ( " . $army_id . ", 2, $wait, $rank )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );				
				
				$query2 = "SELECT army_id " .
						"FROM army " .
						"WHERE follow = $army_id";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$num2 = mysql_numrows( $result2 );
				$i = 0;
				while ( $i < $num2 ) {
					$query_update = "INSERT INTO army_orders ( army_id, order_type, order_value, rank ) " .
							"VALUES ( " . mysql_result( $result2, $i, 'army_id' ) . ", 2, $wait, $rank )";
					mysql_query( $query_update ) or die( "Query failed : " . mysql_error() );				
					$i ++;
				}
				$status = translate( "Wait order added succesfully." );
			}
			else {
				$status = translate( "No valid number of turns entered." );
			}
		}
	}
	else {
		$status = translate( "This armies order queue is full." );
	}
	
	if( $status != "" ) {
		$_SESSION[ 'que_order_status' ] = $status;
	}
	header( "Location: army.php?name=$name" );	
}
else {
	header( "Location: armies.php" );
}
?>
